<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="基本信息ID" prop="basicInfoId">
            <el-input
              v-model="form.basicInfoId"
              placeholder="请输入基本信息ID"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="时间" prop="trainingDate">
            <el-date-picker
              clearable
              v-model="form.trainingDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12"> </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="地点" prop="place">
            <el-input v-model="form.place" placeholder="请输入地点" />
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item label="有无进修证书" prop="hasStudyCertificate">
            <el-select
              v-model="form.hasStudyCertificate"
              placeholder="请选择是否有进修证书"
            >
              <el-option
                v-for="dict in dict.type.has_study_certificate"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="内容">
        <editor v-model="form.content" :min-height="192" />
      </el-form-item>
      <el-form-item label="证书PDF件" prop="certificatePdf">
        <file-upload v-model="form.certificatePdf" />
      </el-form-item>
    </el-form>
    <!-- <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div> -->
  </div>
</template>

<script>
import {
  listEducationTraining,
  getEducationTraining,
  delEducationTraining,
  addEducationTraining,
  updateEducationTraining,
} from "@/api/profession/educationTraining";

export default {
  name: "educationTrainingAdd",
  dicts: ["has_study_certificate"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进修信息表格数据
      educationTrainingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        basicInfoId: null,
        trainingDate: null,
        place: null,
        content: null,
        hasStudyCertificate: null,
        certificatePdf: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        basicInfoId: [
          { required: true, message: "基本信息ID不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询进修信息列表 */
    getList() {
      this.loading = true;
      listEducationTraining(this.queryParams).then((response) => {
        this.educationTrainingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        basicInfoId: null,
        trainingDate: null,
        place: null,
        content: null,
        hasStudyCertificate: null,
        certificatePdf: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加进修信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getEducationTraining(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改进修信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEducationTraining(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEducationTraining(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
